package com.gzy.xuexiziliao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.mapper.DoctorMapper;

import com.gzy.xuexiziliao.pojo.entity.Doctor;
import com.gzy.xuexiziliao.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Override
    public void save(Doctor doctor) {
       //创建时间和更新时间设置好了

        doctor.setAppointment(0);
        doctorMapper.save(doctor);

    }

    @Override
    public void delete(List<Integer> ids) {
        doctorMapper.delete(ids);
    }

    @Override
    public void update(Doctor doctor) {
        doctorMapper.update(doctor);
    }

    @Override
    public Doctor getById(Integer id) {
        Doctor doctor = doctorMapper.getById(id);
        return doctor;
    }

    @Override
    public PageResult page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<Doctor> doctorList=doctorMapper.list(name, gender, begin, end);
        Page<Doctor> p=(Page<Doctor>) doctorList;

        //3.封装对象
        PageResult pageResult=new PageResult(p.getTotal(),p.getResult());
        return pageResult;
    }

    @Override
    public List<Doctor> getByName(String name) {
        if (name != null){
            List<Doctor> doctor = doctorMapper.getByName(name);
            return doctor;
        }
        return null;
    }

    /**
     * 预约
     */
    @Override
    public Doctor Appointment(Doctor doctorinfo) {
        if (doctorinfo.getAppointment() != 1)
        {
            doctorinfo.setAppointment(1);
            doctorinfo.setAppointmentTime(LocalDateTime.now());
            doctorMapper.update(doctorinfo);
            return doctorinfo;
        }
        return null;
    }
    /**
     * 取消预约
     */
    @Override
    public Doctor cancelAppointment(Doctor doctorinfo) {
        if (doctorinfo.getAppointment() != 0)
        {
            doctorinfo.setAppointment(0);
            doctorinfo.setAppointmentTime(null);
            doctorMapper.cancel(doctorinfo);
            return doctorinfo;
        }
        return null;
    }

    @Override
    public PageResult appointmentpage(Integer page, Integer pageSize,int appointment, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<Doctor> doctorList=doctorMapper.appointmentlist(appointment,begin,end);
        Page<Doctor> p=(Page<Doctor>) doctorList;
        //3.封装对象
        PageResult pageBean=new PageResult(p.getTotal(),p.getResult());
        return pageBean;
    }


}
